<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */
// ------------------------------------------------------------------------

/**
 * CodeIgniter Model Class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Libraries
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/libraries/config.html
 */
class CI_Model {

    protected $_tabela;

    /**
     * Constructor
     *
     * @access public
     */
    function __construct() {
        log_message('debug', "Model Class Initialized");
    }

    /**
     * __get
     *
     * Allows models to access CI's loaded classes using the same
     * syntax as controllers.
     *
     * @param	string
     * @access private
     */
    function __get($key) {
        $CI = & get_instance();
        return $CI->$key;
    }

     /**
     * @author Lemoel Marques  <lemoel@gmail.com>
     * @param array $arrayDados recebe um array do tipo campo e valor
     * @return type
     */
    public function insert(Array $arrayDados) {
        return $this->db->insert($this->_tabela, $arrayDados);
    }

    /**
     * @author Lemoel Marques  <lemoel@gmail.com>
     * Método responsável por fazer os select em qualquer tabela
     * @param type $where
     * 
     * Inserindo o where na query
     * Exemplos:
     * $array = array('name' => $name, 'title' => $title, 'status' => $status);
     * Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active' 
     * 
     * $array = array('name != ' => $name, 'title' => $title, 'status ===' => $status);
     * Produces: WHERE name != 'Joe' AND title = 'boss' AND status === 'active' 
     * 
     * @param type $limit contém o limit para nosso select
     * Exemplos: $limit = 10;
     * Produces: LIMIT 10
     * @param type $offset Description
     * @param type $orderby Clausula orderby
     * 
     
     */
    public function select($arrayCol = null, $where = null, $limit = null, $orderby = null) {
        
        /*
         * Inserindo as colunas de retorno na query
         */
        (sizeof($arrayCol) > 0) ? $this->db->select($arrayCol): '';
        
        /*
         * Inserindo o where na query
         * $this->db->where('name', $name);
         * Produces: WHERE name = 'Joe' 
         * /################
         * $this->db->where('name !=', $name);
         * $this->db->where('id <', $id);
         * Produces: WHERE name != 'Joe' AND id < 45 
         * #################################
         * $array = array('name' => $name, 'title' => $title, 'status' => $status);
         * $this->db->where($array);
         * Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active' 
         * #####################################
         * $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
         * $this->db->where($array);
         */
        (!empty($where)) ? $this->db->where($where) : '';
        
        /*
         * Inserindo o limit na query
         * $this->db->limit(10);
         * Produces: LIMIT 10
         */
        (!empty($limit)) ? $this->db->limit($limit) : '';
        
        /*
         * $this->db->order_by("title", "desc");
         * Produces: ORDER BY title DESC 
         */
        (!empty($orderby)) ? $this->db->order_by($orderby) : '' ;
        
        return $this->db->get($this->_tabela);

    }

    /**
     * @author Lemoel Marques  <lemoel@gmail.com>
     * Realiza update na tabela definida na variavel $_tabela
     * @param array $dados Array de Dados
     * @param string $where Coloca o where do usuario
     */
    public function update(Array $dados, $where = null) {
       (!empty($where)) ? $this->db->where($where) : '';
       return $this->db->update($this->_tabela,$dados);
    }

    /**
     * @author Lemoel Marques  <lemoel@gmail.com>
     * Responsável por deletar um registro da tabela
     * @param type $where
     * @return true/false
     */
    public function delete($where) {
        (!empty($where)) ? $this->db->where($where) : '';
        return $this->db->delete($this->_tabela);
    }
}
// END Model Class

/* End of file Model.php */
/* Location: ./system/core/Model.php */